<?php

require_once("inc.member.php");



$this_title="$vars[member_title] &raquo; ".__("Volume");

$page_title=__("Volume");

$content_title=__("Volume");



$item_per_page=$vars["user_item_per_page"];



require_once("inc.desk_select.php");

$max_dlot=mysql_result(mysql_query("select max_dlot from gg_users where id=".$uid), 0) or 0;

$r=mysql_query("select distinct level from gg_member_comm where uid=".$uid." and type='p' order by level");
while($rs = mysql_fetch_assoc($r))
{
	$level[$rs['level']] = $max_dlot;
}

//#####LIST EWALLET HISTORY#####

$count_sql="select count(*) from $db->member_comm where uid='$uid' and type='p'";

$record_sql="select * from $db->member_comm where uid='$uid' and type='p' order by id limit <%limit_start%>, <%limit%>";

$cur_page=$_GET["nav"];

$total_record=@mysql_result(mysql_query($count_sql), 0);

$total_page=ceil($total_record/$item_per_page);

$page=($cur_page? ($cur_page>$total_page? $total_page : ($cur_page<1? 1 : $cur_page)) : 1);

$limit_start=($page-1)*$item_per_page;

$nav_link=format_page($page, $total_page, 15);

$this_page_total=@mysql_num_rows($r=mysql_query(str_replace("<%limit_start%>", $limit_start, str_replace("<%limit%>", $item_per_page, $record_sql))));

$k=0;

for($i=0;$i<$this_page_total;$i++){

 $p=mysql_fetch_assoc($r);

 $level[$p['level']] -= $p['volume_type']/10000;
 
 $on_left=$p['volume_type']/10000;
 if($p['leg2_amount']==$p['volume_type']){
 	$on_right=$on_left;
 	$on_left=0;
 }


//new formula
$arr_volumn = array();

if($get_s['date_from']<>''){
	$level=@mysql_result(mysql_query("select matrix_level from $db->users where id=$uid"), 0);
	$left_id=@mysql_result(mysql_query("select id from $db->users where matrix_upline=$uid and matrix_position=1"), 0);
	$right_id=@mysql_result(mysql_query("select id from $db->users where matrix_upline=$uid and matrix_position=2"), 0);
	
	$previous_date = date('Y-m-d', strtotime('-1 day', strtotime($get_s['date_from'])));
	$left_info=mysql_fetch_assoc(mysql_query("select bv,amount from $db->member_cf where uid=$left_id and cdate='".$previous_date."'"), 0);
	$right_info=mysql_fetch_assoc(mysql_query("select bv,amount from $db->member_cf where uid=$right_id and cdate='".$previous_date."'"), 0);

	$this_page_total=@mysql_num_rows($r=mysql_query("select e.collected_date,e.total_bv,d.code,d.matrix_level from gg_users d inner join gg_users s on s.id={$left_id} and d.matrix_left between s.matrix_left and s.matrix_right inner join gg_purchase e on e.uid=d.id and date_format(e.collected_date, '%Y-%m-%d')='".$get_s['date_from']."'"));
	for($i=0;$i<$this_page_total;$i++){
		$p=mysql_fetch_assoc($r);
		$arr_volumn[$p['code']]['1']['date'] = $p['collected_date'];
		$arr_volumn[$p['code']]['1']['bv'] = $p['total_bv'];
		$arr_volumn[$p['code']]['1']['level'] = $p['matrix_level'];
		$arr_volumn[$p['code']]['2']['date'] = '';
		$arr_volumn[$p['code']]['2']['bv'] = 0;
		$arr_volumn[$p['code']]['2']['level'] = 0;
	}
	
	$this_page_total=@mysql_num_rows($r=mysql_query("select e.collected_date,e.total_bv,d.code,d.matrix_level from gg_users d inner join gg_users s on s.id={$right_id} and d.matrix_left between s.matrix_left and s.matrix_right inner join gg_purchase e on e.uid=d.id and date_format(e.collected_date, '%Y-%m-%d')='".$get_s['date_from']."'"));
	for($i=0;$i<$this_page_total;$i++){
		$p=mysql_fetch_assoc($r);
		$arr_volumn[$p['code']]['2']['date'] = $p['collected_date'];
		$arr_volumn[$p['code']]['2']['bv'] = $p['total_bv'];
		$arr_volumn[$p['code']]['2']['level'] = $p['matrix_level'];
		$arr_volumn[$p['code']]['1']['date'] = '';
		$arr_volumn[$p['code']]['1']['bv'] = 0;
		$arr_volumn[$p['code']]['1']['level'] = 0;
	}
	
	$left_amount = $left_info['amount']/10000;
	$right_amount = $right_info['amount']/10000;
	
	$arr_level = array();
	$this_page_total=@mysql_num_rows($r=mysql_query("select sum(c.bv) as bv, level from gg_users a inner join gg_users b on b.matrix_left between a.matrix_left and a.matrix_right inner join gg_purchase c on c.uid=b.id and date_format(e.collected_date, '%Y-%m-%d')='".$get_s['date_from']."' where a.id=$left_id group by b.matrix_level"), 0);
	for($i=0;$i<$this_page_total;$i++){
		$p=mysql_fetch_assoc($r);
		$arr_level[$p['level']]['1'] = $p['bv'];
	}
	$this_page_total=@mysql_num_rows($r=mysql_query("select sum(c.bv), level from gg_users a inner join gg_users b on b.matrix_left between a.matrix_left and a.matrix_right inner join gg_purchase c on c.uid=b.id and date_format(e.collected_date, '%Y-%m-%d')='".$get_s['date_from']."' where a.id=$right_id group by b.matrix_level"), 0);
	for($i=0;$i<$this_page_total;$i++){
		$p=mysql_fetch_assoc($r);
		$arr_level[$p['level']]['2'] = $p['bv'];
	}	
	
	ksort($arr_volumn);
	foreach ($arr_volumn as $uid => $value) {
		//$left_amount += ($value['1']['bv']/100);
		//$right_amount += ($value['2']['bv']/100);
		$arr_level[$value['1']['level']]['1'] += ($value['1']['bv']/100);
		$arr_level[$value['2']['level']]['2'] += ($value['2']['bv']/100);
				
		$his_row.="

		 <tr class='row$k'>
		
		  <td>".($value['1']['bv']>0?$value['1']['date']:$value['2']['date'])."</td>
		
		  <td>".$uid."</td>
		
		  <td class='number'>".($value['1']['bv']/100)."</td>
		
		  <td class='number'>".($value['2']['bv']/100)."</td>
		
		  <td class='number'>".(($value['1']['level']+$value['2']['level'])-$level)."</td>
		
		  <td class='number'>".($arr_level[$value['1']['level']]['1']+$arr_level[$value['2']['level']]['1'])."/".$max_dlot."</td>
		
		  <td class='number'>".($arr_level[$value['1']['level']]['2']+$arr_level[$value['2']['level']]['2'])."/".$max_dlot."</td>
		
		 </tr>";
		
		 $k=1-$k;
	}
}

/*
 $his_row.="

 <tr class='row$k'>

  <td>".($i+1)."</td>

  <td>".$p["bonus_date"]."</td>

  <td>".$p["descr"]."</td>

  <td class='number'>".$on_left."</td>

  <td class='number'>".$on_right."</td>

  <td class='number'>".$p['level']."</td>

  <td class='number'>".($p['leg1']==1?(($p['leg1_amount']-$p['volume_type'])/10000):(($p['leg2_amount']-$p['volume_type'])/10000))."/".$max_dlot."</td>

  <td class='number'>".($p['leg2']==1?(($p['leg1_amount']-$p['volume_type'])/10000):(($p['leg2_amount']-$p['volume_type'])/10000))."/".$max_dlot."</td>

 </tr>";

 $k=1-$k;
*/
}

 

$r_month=array('1','2','3','4','5','6','7','8','9','10','11','12');

$r_month_d=array('1'=>__("1"),'2'=>__("2"),'3'=>__("3"),'4'=>__("4"),'5'=>__("5"),'6'=>__("6"),'7'=>__("7"),'8'=>__("8"),'9'=>__("9"),'10'=>__("10"),'11'=>__("11"),'12'=>__("12"));

for($i=2009,$j=0,$t=ndate("Y");$i<=$t;$i++){

 $r_year[$j++]=$i;

 $r_year_d[$i]=$i;

}


?>
<link rel="stylesheet" href="<?php echo CSS_URL; ?>/jstheme/ui-lightness/jquery-ui.css" type="text/css" />
<?php
//css
ob_start();
$css .= ob_get_contents();
ob_end_clean();


//listing text

$listing_text=replace_tag(__("<%x%> <%title%> <%record%>."), array("<%x%>"=>"$total_record", "<%title%>"=>__('volume'), "<%record%>"=>$total_record>1? __("records") : __("record")));



//listing

$his_list=

"<h2>".replace_tag(__("<%title%>"), array("<%title%>"=>__('Volume')))."</h2>

$listing_text $nav_link

<table class='pbl_table'>

 <tr class='pbl_header'>

  <td>".__("Date")."</td>

  <td>".__("Member ID")."</td>

  <td>".__("On Left")."</td>

  <td>".__("On Right")."</td>

  <td>".__("At Level")."</td>

  <td>".__("Level Left")."</td>

  <td>".__("Level Right")."</td>

 </tr>

 $his_row

</table>

$nav_link";

$search = "<form name='comm_form' method='get' action='$this_file'>

<input type='hidden' name='__req' value='1' />

<input type='hidden' name='nav' value=\"$get_h[nav]\" />

<table class='pbt_table'>

	<tr>

		<td>".__("Date").__(":")." <input type='text' name='date_from' id='date_from' value=\"$get_h[date_from]\" /><input type='submit' value=\"".__("Search")."\" /></td>

	</tr>

</table>

</form>";

$content=$inc_wallet_select.$search.$his_list;

print format_member_page($content, $this_title, $content_title, $css.$inc_jvscript);

?>